﻿package com.game.blackwhite;

import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.*;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;

import com.game.engine.*;
//import com.game.Chess.*;
//import com.game.ai.*;

public class BlackWhite extends Activity {

	final static int cmNone  = 0;
	final static int cmBlack = -1;
	final static int cmWhite = 1;
	static int East= 0;
	static int North= 1;
	static int West= 2;
	static int South= 3;
	final static int csComputer = cmBlack;//cmWhite;
	final static int csYou      = cmWhite;//cmBlack;
	final static int OpponentAI = 0;
	final static int OpponentHuman = 1;
	public static int AI_Human_Flag;
	TileView mView; 
    CharSequence strHint = "", strGoal = "";
	TextView mStatusText;
	//Chess C;
	
	//AI mAI;

	
	
	//此处为菜单按钮
	public static final int ITEM0 = Menu.FIRST;
	public static final int ITEM1 = Menu.FIRST + 1;
	
	public int act = csYou;
	public int going = 0;
	public int gosucc = 0;
	public int moving = 0;

	class ChessTable {
		public int[][] table;
		public int[][] imagetable;
		public int[][] backtable;
		public int level;
		
		public final int Rows=4;
		public final int Cols=4;
		
		public int start_row = 0;
		public int start_col = 0;
		public int end_row = 0;
		public int end_col = 0;
		
		public int start_x = 0;
		public int start_y = 0;
		
		public ChessTable() {
			table = new int[4][4];
			imagetable = new int[4][4];
			backtable = new int[4][4];
	        for(int i=0;i<4;i++)
	        	for(int j=0;j<4;j++)
	        	{
	        		table[i][j]=cmNone;
	        	}
	        table[0][0]=cmBlack;
	        table[0][1]=cmBlack;
	        table[0][2]=cmBlack;
	        table[0][3]=cmBlack;
	        table[1][0]=cmBlack;
	        table[1][3]=cmBlack;
	        table[2][0]=cmWhite;
	        table[2][3]=cmWhite;
	        table[3][0]=cmWhite;
	        table[3][1]=cmWhite;
	        table[3][2]=cmWhite;
	        table[3][3]=cmWhite;
	        
	        level = 3;
		}
		public void init()
		{
			start_row = 0;
			start_col = 0;
			end_row = 0;
			end_col = 0;
			
			start_x = 0;
			start_y = 0;
			
			for(int i=0;i<4;i++)
	        	for(int j=0;j<4;j++)
	        	{
	        		table[i][j]=cmNone;
	        	}
	        table[0][0]=cmBlack;
	        table[0][1]=cmBlack;
	        table[0][2]=cmBlack;
	        table[0][3]=cmBlack;
	        table[1][0]=cmBlack;
	        table[1][3]=cmBlack;
	        table[2][0]=cmWhite;
	        table[2][3]=cmWhite;
	        table[3][0]=cmWhite;
	        table[3][1]=cmWhite;
	        table[3][2]=cmWhite;
	        table[3][3]=cmWhite;
			
		}
		public int GetGoal(int[] goal) //0=you, 1=computer, 2=empty
		{
		  int i,j;
		  goal[0]=0;
		  goal[1]=0;
		  goal[2]=0;
		  for(i=0;i<Rows;i++)
		    for(j=0;j<Cols;j++)
		    {
		       if(table[i][j]==cmWhite)(goal[0])++;
		       else if(table[i][j]==cmBlack)(goal[1])++;
		       else (goal[2])++;
		    }
		  return 0;
		}
		public void Set(int[][] tab)
		{
			int i, j;
			for (i=0; i<4; i++) {
				for(j=0; j<4; j++) {
					imagetable[i][j] = tab[i][j];
				}
			}
		}
		
		public void Set_tab()
		{
			int i, j;
			for (i=0; i<4; i++) {
				for(j=0; j<4; j++) {
					table[i][j] = imagetable[i][j];
				}
			}
		}
		
		public void Table_to_image()
		{
			int i, j;
			for (i=0; i<4; i++) {
				for(j=0; j<4; j++) {
					imagetable[i][j] = table[i][j];
				}
			}
		}
		public void goback()
		{
			int i, j;
			for (i=0; i<4; i++) {
				for(j=0; j<4; j++) {
					table[i][j] = backtable[i][j];
				}
			}
		}
		
		public void setbacktable()
		{
			int i, j;
			for (i=0; i<4; i++) {
				for(j=0; j<4; j++) {
					backtable[i][j] = table[i][j];
				}
			}
		}
	}
	

	ChessTable mTable;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.startoptions);     
        AddStartOptions();   
      
        
        /**/
  }
    
   
	
    
    public void AddStartOptions(){
    	//此处为开始界面选择
    	Button HumanAI = (Button)findViewById(R.id.HumanAI);
    	Button HumanHuman = (Button)findViewById(R.id.HumanHuman);
    	Button Instructions = (Button)findViewById(R.id.Instruction);
    	HumanAI.setOnClickListener(new Button.OnClickListener(){
    		public void onClick(View v){
    			AI_Human_Flag = OpponentAI;
    			setContentView(R.layout.main);    	
    			Resources r = getResources();
    	        mView = (TileView)findViewById(R.id.View01);
    	        mStatusText = (TextView)findViewById(R.id.text);
    	       
    	        mView.resetTiles(3);
    	        mView.loadTile(0, r.getDrawable(R.drawable.emp));
    	        mView.loadTile(1, r.getDrawable(R.drawable.black));
    	        mView.loadTile(2, r.getDrawable(R.drawable.white));
    	    
    	        mTable = new ChessTable();
    	        displayTable(mView, mTable.table);
    	        
    	        strHint = "White go now !!";
    	        
    	        mStatusText.setText(strHint);
    	        
    	        Button SrartB = (Button)findViewById(R.id.StartButton);
    	    	Button AiB = (Button)findViewById(R.id.ChooseOpponentLeft);
    	    	Button HumanB = (Button)findViewById(R.id.ChooseOpponentRight);
    	    	Button GoBackB = (Button)findViewById(R.id.GoBack);
    	    	


	    	    		SrartB.setOnClickListener(new Button.OnClickListener(){
	    	        		public void onClick(View v){
	    	        			
	    	        			act = csYou;
	    	        			mTable.init();
	    	        			
	    	        			displayTable(mView, mTable.table);
	    	        	        
	    	        	        strHint = "White go now !!";
	    	        	        
	    	        	        mStatusText.setText(strHint);
	    	        			
	    	        		}
	    	        	});
	    	    		
	    	    		AiB.setOnClickListener(new Button.OnClickListener(){
	    	        		public void onClick(View v){
	    	        			
	    	        			AI_Human_Flag = OpponentAI;
	    	        			act = csYou;
	    	        			mTable.init();
	    	        			
	    	        			displayTable(mView, mTable.table);
	    	        	        
	    	        	        strHint = "White go now !!";
	    	        	        
	    	        	        mStatusText.setText(strHint);
	    	        			
	    	        		}
	    	        	});
	    	    		HumanB.setOnClickListener(new Button.OnClickListener(){
	    	        		public void onClick(View v){
	    	        			
	    	        			AI_Human_Flag = OpponentHuman;
	    	        			act = csYou;
	    	        			mTable.init();
	    	        			
	    	        			displayTable(mView, mTable.table);
	    	        	        
	    	        	        strHint = "White go now !!";
	    	        	        
	    	        	        mStatusText.setText(strHint);
	    	        			
	    	        		}
	    	        	});
	    	    		
	    	    		GoBackB.setOnClickListener(new Button.OnClickListener(){
	    	        		public void onClick(View v){
	    	        			
	    	        			if(AI_Human_Flag == OpponentHuman)
	    	        				act = -act;
	    	        			else
	    	        				act = csYou;
	    	        			
	    	        			mTable.goback();
	    	        			
	    	        			
	    	        			displayTable(mView, mTable.table);
	    	        	        
	    	        	        strHint = "White go now !!";
	    	        	        
	    	        	        mStatusText.setText(strHint);
	    	        			
	    	        		}
	    	        	});
    		}
    	});
    	
    	HumanHuman.setOnClickListener(new Button.OnClickListener(){
    		public void onClick(View v){
    			AI_Human_Flag = OpponentHuman;
    			setContentView(R.layout.main);    			
    			Resources r = getResources();
    	        mView = (TileView)findViewById(R.id.View01);
    	        mStatusText = (TextView)findViewById(R.id.text);
    	       
    	        mView.resetTiles(3);
    	        mView.loadTile(0, r.getDrawable(R.drawable.emp));
    	        mView.loadTile(1, r.getDrawable(R.drawable.black));
    	        mView.loadTile(2, r.getDrawable(R.drawable.white));
    	    
    	        mTable = new ChessTable();
    	        displayTable(mView, mTable.table);
    	        
    	        strHint = "White go now !!";
    	        
    	        mStatusText.setText(strHint);
    	        
    	        Button SrartB = (Button)findViewById(R.id.StartButton);
    	    	Button AiB = (Button)findViewById(R.id.ChooseOpponentLeft);
    	    	Button HumanB = (Button)findViewById(R.id.ChooseOpponentRight);
    	    	Button GoBackB = (Button)findViewById(R.id.GoBack);
    	    	


	    	    		SrartB.setOnClickListener(new Button.OnClickListener(){
	    	        		public void onClick(View v){
	    	        			
	    	        			act = csYou;
	    	        			mTable.init();
	    	        			
	    	        			displayTable(mView, mTable.table);
	    	        	        
	    	        	        strHint = "White go now !!";
	    	        	        
	    	        	        mStatusText.setText(strHint);
	    	        			
	    	        		}
	    	        	});
	    	    		
	    	    		AiB.setOnClickListener(new Button.OnClickListener(){
	    	        		public void onClick(View v){
	    	        			
	    	        			AI_Human_Flag = OpponentAI;
	    	        			act = csYou;
	    	        			mTable.init();
	    	        			
	    	        			displayTable(mView, mTable.table);
	    	        	        
	    	        	        strHint = "White go now !!";
	    	        	        
	    	        	        mStatusText.setText(strHint);
	    	        			
	    	        		}
	    	        	});
	    	    		HumanB.setOnClickListener(new Button.OnClickListener(){
	    	        		public void onClick(View v){
	    	        			
	    	        			AI_Human_Flag = OpponentHuman;
	    	        			act = csYou;
	    	        			mTable.init();
	    	        			
	    	        			displayTable(mView, mTable.table);
	    	        	        
	    	        	        strHint = "White go now !!";
	    	        	        
	    	        	        mStatusText.setText(strHint);
	    	        			
	    	        		}
	    	        	});
	    	    		
	    	    		GoBackB.setOnClickListener(new Button.OnClickListener(){
	    	        		public void onClick(View v){
	    	        			
	    	        			if(AI_Human_Flag == OpponentHuman)
	    	        				act = -act;
	    	        			else
	    	        				act = csYou;
	    	        			
	    	        			mTable.goback();
	    	        			
	    	        			
	    	        			displayTable(mView, mTable.table);
	    	        	        
	    	        	        strHint = "White go now !!";
	    	        	        
	    	        	        mStatusText.setText(strHint);
	    	        			
	    	        		}
	    	        	});
    	        
    	      
    		}
    	});
    	
    	Instructions.setOnClickListener(new Button.OnClickListener(){
    		public void onClick(View v){
    			setContentView(R.layout.instructiontextview);
    		}
    	});
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	
    	int [] loa;
		loa = new int[2];
		mView.getLocationOnScreen(loa);
		
		int row,col, x, y;
		x = (int) event.getX()-loa[0];
		y = (int) event.getY()-loa[1];

		col = (int) Math.floor(x/mView.GetmTileSize());
		row = (int) Math.floor(y/mView.GetmTileSize());
		
		int events[] = {MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE,
				MotionEvent.ACTION_UP};
    	
    	if(AI_Human_Flag==OpponentHuman)
    	{
			
			if(event.getAction()==events[0])
			{
				Log.i("onTouchEvent","MotionEvent.ACTION_DOWN"+event.getAction());
				
				mTable.start_col = col;
				mTable.start_row = row;
				mTable.start_x = x;
				mTable.start_y = y;
				
				if(mTable.table[row][col]==act)
				{
					going = 1;
					moving = 1;
					mView.Getmoving(x-mTable.start_x, y-mTable.start_y, mTable.start_col, mTable.start_row, moving);
				}
				else
				{
					going = 0;
					moving = 0;
					mView.Getmoving(0, 0, 0, 0, 0);
					if(act==cmWhite)
						strHint = "you cann't go this!White go now!!";
					else if(act==cmBlack)
						strHint = "you cann't go this!Black go now!!";
			        mStatusText.setText(strHint);
				}
				
				Log.i("onTouchEvent","going------"+going);
			}
			if(event.getAction()==events[1])
			{
				Log.i("onTouchEvent","MotionEvent.ACTION_MOVE"+event.getAction());
				
				if(moving == 1)
				{
					mView.Getmoving(x-mTable.start_x, y-mTable.start_y, mTable.start_col, mTable.start_row, moving);
					
					displayTable(mView, mTable.table);
					
				}
				else
				{
					mView.Getmoving(0, 0, 0, 0, 0);
				}
			}
			if(event.getAction()==events[2])
			{
				Log.i("onTouchEvent","MotionEvent.ACTION_UP"+event.getAction());
				
				moving = 0;
				mView.Getmoving(0, 0, 0, 0, 0);
				
				mTable.end_col = col;
				mTable.end_row = row;
				
				Log.i("onTouchEvent","MotionEvent.ACTION_UP----going------"+going);
				
				if(going==1)
				{
					going = 0;
					gosucc = 0;
					
					if(mTable.end_col==mTable.start_col)
					{
						if((mTable.end_row == (mTable.start_row -1)) || (mTable.end_row == (mTable.start_row +1)))
							if(mTable.table[mTable.end_row][mTable.end_col]==cmNone)
								gosucc = 1;
					}
					if(mTable.end_row==mTable.start_row)
					{
						if((mTable.end_col == (mTable.start_col -1)) || (mTable.end_col == (mTable.start_col +1)))
							if(mTable.table[mTable.end_row][mTable.end_col]==cmNone)
								gosucc = 1;
					}
				
					Log.i("onTouchEvent","MotionEvent.ACTION_UP----gosucc------"+gosucc);
					
					if(gosucc==1)
					{
						gosucc = 0;
						
						act = -act;
						mTable.setbacktable();
						ChangeChess(mView,mTable.table);
						
						
						if(act==cmWhite)
							strHint = "White go now !!";
						else if(act==cmBlack)
							strHint = "Black go now !!";
				        mStatusText.setText(strHint);
				        
				        
					}
					else
					{
						if(act==cmWhite)
							strHint = "you cann't here!!White go now!!";
						else if(act==cmBlack)
							strHint = "you cann't here!!Black go now!!";
				        mStatusText.setText(strHint);
					}
					
					
				}
				else
				{
					if(act==cmWhite)
						strHint = "you cann't here!!White go now !!";
					else if(act==cmBlack)
						strHint = "you cann't here!!Black go now !!";
			        mStatusText.setText(strHint);
				}
				
				int over;
				over = GameOver(mTable.table);
				
				if(over==cmWhite)
				{
					strHint = "White Win!!Game over!!";
					mStatusText.setText(strHint);
				}
				else if(over==cmBlack)
				{
					strHint = "Black Win!!Game over!!";
					mStatusText.setText(strHint);
				}
			}
			
			Log.i("onTouchEvent","event.getX()"+col);
			Log.i("onTouchEvent","event.getY()"+row);
	       // System.out.println("坐标:" + x + "," + y);
	
	       
    	}
    	else if(AI_Human_Flag==OpponentAI)
    	{
    		if(act==csYou)
    		{
    			if(event.getAction()==events[0])
    			{
    				Log.i("onTouchEvent","MotionEvent.ACTION_DOWN"+event.getAction());
    				
    				mTable.start_col = col;
    				mTable.start_row = row;
    				mTable.start_x = x;
    				mTable.start_y = y;
    				
    				if(mTable.table[row][col]==act)
    				{
    					going = 1;
    					moving = 1;
    					mView.Getmoving(x-mTable.start_x, y-mTable.start_y, mTable.start_col, mTable.start_row, moving);
    				}
    				else
    				{
    					going = 0;
    					moving = 0;
    					mView.Getmoving(0, 0, 0, 0, 0);
    					if(act==cmWhite)
    						strHint = "you cann't go this!White go now!!";
    					else if(act==cmBlack)
    						strHint = "you cann't go this!Black go now!!";
    			        mStatusText.setText(strHint);
    				}
    				
    				Log.i("onTouchEvent","going------"+going);
    			}
    			if(event.getAction()==events[1])
    			{
    				Log.i("onTouchEvent","MotionEvent.ACTION_MOVE"+event.getAction());
    				
    				if(moving == 1)
    				{
    					mView.Getmoving(x-mTable.start_x, y-mTable.start_y, mTable.start_col, mTable.start_row, moving);
    					
    					displayTable(mView, mTable.table);
    					
    				}
    				else
    				{
    					mView.Getmoving(0, 0, 0, 0, 0);
    				}
    			}
    			if(event.getAction()==events[2])
    			{
    				Log.i("onTouchEvent","MotionEvent.ACTION_UP"+event.getAction());
    				
    				moving = 0;
    				mView.Getmoving(0, 0, 0, 0, 0);
    				
    				mTable.end_col = col;
    				mTable.end_row = row;
    				
    				Log.i("onTouchEvent","MotionEvent.ACTION_UP----going------"+going);
    				
    				if(going==1)
    				{
    					going = 0;
    					gosucc = 0;
    					
    					if(mTable.end_col==mTable.start_col)
    					{
    						if((mTable.end_row == (mTable.start_row -1)) || (mTable.end_row == (mTable.start_row +1)))
    							if(mTable.table[mTable.end_row][mTable.end_col]==cmNone)
    								gosucc = 1;
    					}
    					if(mTable.end_row==mTable.start_row)
    					{
    						if((mTable.end_col == (mTable.start_col -1)) || (mTable.end_col == (mTable.start_col +1)))
    							if(mTable.table[mTable.end_row][mTable.end_col]==cmNone)
    								gosucc = 1;
    					}
    				
    					Log.i("onTouchEvent","MotionEvent.ACTION_UP----gosucc------"+gosucc);
    					
    					if(gosucc==1)
    					{
    						gosucc = 0;
    						
    						act = -act;
    						mTable.setbacktable();
    						//human go success
    						ChangeChess(mView,mTable.table);
    						
    						
    						if(act==cmWhite)
    							strHint = "AI: White go now !!";
    						else if(act==cmBlack)
    							strHint = "AI: Black go now !!";
    				        mStatusText.setText(strHint);
    				        
    				        
    				        Log.i("AINext","++++ calling  AI   ++++++++");
    				        // the AI function call here
    				        
    				        int ai_startrow;
    				        int ai_startcol;
    				        int ai_endrow;
    				        int ai_endcol;
    				        int[] ai_run = new int[4];
    				        
    				        AINext(act,mTable.table,ai_run);
    				        //act,mTable.table,
    				        
    				      //  ai_run[0] = 1;
    					//	ai_run[1] = 0;
    						//ai_run[2] = 1;
    					//	ai_run[3] = 1;
    				        
    				        mTable.start_row = ai_run[0];
    				        mTable.start_col = ai_run[1];
    				        mTable.end_row = ai_run[2];
    				        mTable.end_col = ai_run[3];
    				        
    				        Log.i("AINext","++++ start_row    ++++++++"+ai_run[0]);
    				        Log.i("AINext","++++ start_col    ++++++++"+ai_run[1]);
    				        Log.i("AINext","++++ end_row    ++++++++"+ai_run[2]);
    				        Log.i("AINext","++++ end_col    ++++++++"+ai_run[3]);
    				        
    				       
    				        ChangeChess(mView,mTable.table);
    				        
    				        act = -act;
    				        if(act==cmWhite)
    							strHint = "You: White go now !!";
    						else if(act==cmBlack)
    							strHint = "You: Black go now !!";
    				        mStatusText.setText(strHint);
    				        
    				        
    					}
    					else
    					{
    						if(act==cmWhite)
    							strHint = "you cann't here!!White go now!!";
    						else if(act==cmBlack)
    							strHint = "you cann't here!!Black go now!!";
    				        mStatusText.setText(strHint);
    					}
    					
    					
    				}
    				else
    				{
    					if(act==cmWhite)
    						strHint = "you cann't here!!White go now !!";
    					else if(act==cmBlack)
    						strHint = "you cann't here!!Black go now !!";
    			        mStatusText.setText(strHint);
    				}
    				
    				int over;
    				over = GameOver(mTable.table);
    				
    				if(over==cmWhite)
    				{
    					strHint = "White Win!!Game over!!";
    					mStatusText.setText(strHint);
    				}
    				else if(over==cmBlack)
    				{
    					strHint = "Black Win!!Game over!!";
    					mStatusText.setText(strHint);
    				}
    			}
    			
    			Log.i("onTouchEvent","event.getX()"+col);
    			Log.i("onTouchEvent","event.getY()"+row);
    		}
    		else
    		{
    			strHint = "You can't go now!";
				mStatusText.setText(strHint);
    		}
    		
    	}
    	
    	 return super.onTouchEvent(event);
    }
      
    void displayTable(TileView view, int[][] table)
    {
        for(int col=0;col<4;col++)
        	for(int row=0;row<4;row++)
        	{
        		if(table[row][col]==cmBlack)
        			view.setTile(1, col, row);
        		else if(table[row][col]==cmWhite)
        			view.setTile(2, col, row);
        		else 
        			view.setTile(0, col, row);
        	}
        view.invalidate();
    }
    
    void ChangeChess(TileView view,int[][] table)
    {
    
    	
    	
    		table[mTable.end_row][mTable.end_col] = table[mTable.start_row][mTable.start_col];
    		table[mTable.start_row][mTable.start_col] = cmNone;
    	
    	EatChess(mTable.table);
    	
    	displayTable(mView, mTable.table);
    }
    
    void EatChess(int[][] table)
    {
    	int row = mTable.end_row;
    	int col = mTable.end_col;
    	
    	int you = table[mTable.end_row][mTable.end_col];
    	int other = -you;
    	
    	
    	if(table[row][0]==other && table[row][1]==you && table[row][2]==you && table[row][3]!=other)
    		table[row][0]=cmNone;
    	if(table[row][1]==other && table[row][0]==you && table[row][2]==you && table[row][3]!=other)
    		table[row][1]=cmNone;
    	if(table[row][1]==other && table[row][2]==you && table[row][3]==you && table[row][0]!=other)
    		table[row][1]=cmNone;
    	if(table[row][2]==other && table[row][1]==you && table[row][3]==you && table[row][0]!=other)
    		table[row][2]=cmNone;
    	if(table[row][2]==other && table[row][0]==you && table[row][1]==you && table[row][3]!=other)
    		table[row][2]=cmNone;
    	if(table[row][3]==other && table[row][1]==you && table[row][2]==you && table[row][0]!=other)
    		table[row][3]=cmNone;
    		
    
    	if(table[0][col]==other && table[1][col]==you && table[2][col]==you && table[3][col]!=other)
    		table[0][col]=cmNone;
    	if(table[1][col]==other && table[0][col]==you && table[2][col]==you && table[3][col]!=other)
    		table[1][col]=cmNone;
    	if(table[1][col]==other && table[2][col]==you && table[3][col]==you && table[0][col]!=other)
    		table[1][col]=cmNone;
    	if(table[2][col]==other && table[1][col]==you && table[3][col]==you && table[0][col]!=other)
    		table[2][col]=cmNone;
    	if(table[2][col]==other && table[0][col]==you && table[1][col]==you && table[3][col]!=other)
    		table[2][col]=cmNone;
    	if(table[3][col]==other && table[1][col]==you && table[2][col]==you && table[0][col]!=other)
    		table[3][col]=cmNone;
    	
    	
    }
    
    int GameOver(int[][] table)
    {
    	int[] goal=new int[3];
    	mTable.GetGoal(goal);
    	
    	if(goal[0]<2 && goal[1]>goal[0])
    		return cmBlack;
    	
    	if(goal[1]<2 && goal[0]>goal[1])
    		return cmWhite;
    	
    	return cmNone;
    }
    
    /*
    public int OppoAINext(Chess C, int color)
    {
    	Chess ImagChess;
    	ImagChess.SetChess(C);
    	
    	int i, j, k;
    	int rcolor = (-1)*color;
    	//检查能否吃到子
    	for(i=0; i<4; i++) {
    		for(j=0; j<4; j++) {
    			if(ImagChess.Pieces[i][j] == color) {
    				for(k=0; k<4; k++) {
    					if(ImagChess.MoveCheck(i, j, k) == 1) {
    						return 1;
    					} else {
    						ImagChess.Set(C);
    					}
    				}
    			}
    		}
    	}

    	return 0;
    }
    //color为AI的棋子颜色
    Chess AINext(Chess C, int color)
    {
    	Chess ImagChess;
    	ImagChess.SetChess(C);
    	int i, j, k;
    	int rcolor = (-1)*color;
    	//检查能否吃到子
    	for(i=0; i<4; i++) {
    		for(j=0; j<4; j++) {
    			if(ImagChess.Pieces[i][j] == color) {
    				for(k=0; k<4; k++) {
    					if(ImagChess.MoveCheck(i, j, k) == 1) {
    						return ImagChess;
    					} else {
    						ImagChess.Set(C);
    					}
    				}
    			}
    		}
    	}
    	//如果吃不到子，则找出下出一步后不会被对手吃子的那一步
    	for(i=0; i<4; i++) {
    		for(j=0; j<4; j++) {
    			if(ImagChess.Pieces[i][j] == color) {
    				for(k=0; k<4; k++) {
    					if (ImagChess.Move(i, j, k) == 1) {
    						if(OppoAINext(ImagChess, rcolor) == 1) {
    							ImagChess.Set(C);
    						} else {
    							return ImagChess;
    						}
    					}
    				}
    			}
    		}
    	}
    	//如果无法避免被吃子，则按数组顺序走第一个子
    	for(i=0; i<4; i++) {
    		for(j=0; j<4; j++) {
    			if(ImagChess.Pieces[i][j] == color) {
    				for(k=0; k<4; k++) {
    					if (ImagChess.Move(i, j, k) == 1) {
    						return ImagChess;
    					}
    				}
    			}
    		}
    	}
    }
    
   */  
    
    public void AINext(int act, int[][] table, int[] ai_run)
    {
    	 int ai_startrow;
	     int ai_startcol;
	     int ai_endrow;
	     int ai_endcol;
	     
	     
    	 //	ai_run[0] = 1;
			//ai_run[1] = 0;
		//	ai_run[2] = 1;
			//ai_run[3] = 1;
			
			int color = act;
			mTable.Set(table);
			mTable.Table_to_image();
			
			Log.i("AINext","++++ act    ++++++++"+act);
		
			//Chess ImagChess(C);
			int i, j, k;
			int rcolor = (-1)*color;
			
			int[] move_to = new int[3];
			
			for(i=0; i<4; i++)
			{
				for(j=0; j<4; j++) 
				{
					mTable.Table_to_image();
					if(mTable.imagetable[i][j] == color) 
					{
						for(k=0; k<4; k++) 
						{
							for(int u=0;u<3;u++)
					    	{
								move_to[u] = 0;
					    	}
							MoveCheck(i, j, k, mTable.imagetable, move_to);
							if ( move_to[0] == 1) {
								ai_run[0] = i;
								ai_run[1] = j;
								ai_run[2] = move_to[1];
								ai_run[3] = move_to[2];
								return;
							}
							else
							{
								mTable.Set(table);
								mTable.Table_to_image();
							}
						}
					}
				}
			}
			
			
			for(i=0; i<4; i++)
			{
				for(j=0; j<4; j++)
				{
					mTable.Table_to_image();
					if(mTable.imagetable[i][j] == color)
					{
						for(k=0; k<4; k++) 
						{
							for(int u=0;u<3;u++)
					    	{
								move_to[u] = 0;
					    	}
							Move(i, j, k, mTable.imagetable, move_to);
							if ( move_to[0] == 1) {
								ai_run[0] = i;
								ai_run[1] = j;
								ai_run[2] = move_to[1];
								ai_run[3] = move_to[2];
								return;
							}
							else
							{
								mTable.Set(table);
								mTable.Table_to_image();
							}
						}
					}
				}
			}

    }
    
    
    
    /*移动棋子并检查能否吃子*/
    public void MoveCheck(int i, int j, int direc, int[][] Pieces, int[] flag)
    {
    	for(int u=0;u<3;u++)
    	{
    		flag[u] = 0;
    	}
    	switch (direc) {
    	case 0:
    		if (j<3 && Pieces[i][j+1] == cmNone) {
    			Pieces[i][j+1] = Pieces[i][j];
    			Pieces[i][j] = 0;
    			flag[0] = Check(i, j+1, Pieces);
    			flag[1] = i;
    			flag[2] = j+1;
    		}
    		break;
    	case 1:
    		if (i>0 && Pieces[i-1][j] == cmNone) {
    			Pieces[i-1][j] = Pieces[i][j];
    			Pieces[i][j] = 0;
    			flag[0] = Check(i-1, j, Pieces);
    			flag[1] = i-1;
    			flag[2] = j;
    		}
    		break;
    	case 2:
    		if (j>0 && Pieces[i][j-1] == cmNone) {
    			Pieces[i][j-1] = Pieces[i][j];
    			Pieces[i][j] = 0;
    			flag[0] = Check(i, j-1, Pieces);
    			flag[1] = i;
    			flag[2] = j-1;
    		}
    		break;
    	case 3:
    		if (i<3 && Pieces[i+1][j] == cmNone) {
    			Pieces[i+1][j] = Pieces[i][j];
    			Pieces[i][j] = 0;
    			flag[0] = Check(i+1, j, Pieces);
    			flag[1] = i+1;
    			flag[2] = j;
    		}
    	}

    }
    
    public int Check(int row, int col, int[][] table)
    {
    	
    	
    	int you = table[row][col];
    	int other = -you;
    	
    	
    	if(table[row][0]==other && table[row][1]==you && table[row][2]==you && table[row][3]!=other)
    		return 1;
    	if(table[row][1]==other && table[row][0]==you && table[row][2]==you && table[row][3]!=other)
    		return 1;
    	if(table[row][1]==other && table[row][2]==you && table[row][3]==you && table[row][0]!=other)
    		return 1;
    	if(table[row][2]==other && table[row][1]==you && table[row][3]==you && table[row][0]!=other)
    		return 1;
    	if(table[row][2]==other && table[row][0]==you && table[row][1]==you && table[row][3]!=other)
    		return 1;
    	if(table[row][3]==other && table[row][1]==you && table[row][2]==you && table[row][0]!=other)
    		return 1;
    		
    
    	if(table[0][col]==other && table[1][col]==you && table[2][col]==you && table[3][col]!=other)
    		return 1;
    	if(table[1][col]==other && table[0][col]==you && table[2][col]==you && table[3][col]!=other)
    		return 1;
    	if(table[1][col]==other && table[2][col]==you && table[3][col]==you && table[0][col]!=other)
    		return 1;
    	if(table[2][col]==other && table[1][col]==you && table[3][col]==you && table[0][col]!=other)
    		return 1;
    	if(table[2][col]==other && table[0][col]==you && table[1][col]==you && table[3][col]!=other)
    		return 1;
    	if(table[3][col]==other && table[1][col]==you && table[2][col]==you && table[0][col]!=other)
    		return 1;
    	
    	return 0;
    }
    
    
    public void Move(int i, int j, int direc, int[][] Pieces, int[] flag)
    {
    	
    	for(int u=0;u<3;u++)
    	{
    		flag[u] = 0;
    	}
    	switch (direc) {
    	case 0:
    		if (j<3 && Pieces[i][j+1] == cmNone) {
    			Pieces[i][j+1] = Pieces[i][j];
    			Pieces[i][j] = 0;
    			flag[0] = 1;
    			flag[1] = i;
    			flag[2] = j+1;
    		}
    		break;
    	case 1:
    		if (i>0 && Pieces[i-1][j] == cmNone) {
    			Pieces[i-1][j] = Pieces[i][j];
    			Pieces[i][j] = 0;
    			flag[0] = 1;
    			flag[1] = i-1;
    			flag[2] = j;
    		}
    		break;
    	case 2:
    		if (j>0 && Pieces[i][j-1] == cmNone) {
    			Pieces[i][j-1] = Pieces[i][j];
    			Pieces[i][j] = 0;
    			flag[0] = 1;
    			flag[1] = i;
    			flag[2] = j-1;
    		}
    		break;
    	case 3:
    		if (i<3 && Pieces[i+1][j] == cmNone) {
    			Pieces[i+1][j] = Pieces[i][j];
    			Pieces[i][j] = 0;
    			flag[0] = 1;
    			flag[1] = i+1;
    			flag[2] = j;
    		}
    		break;
    	}
    }
    
    
    
    /*
    public int MoveCheck(int i, int j, int direc)
    {
    	int flag = 0;
    	switch (direc) {
    	case East:
    		if (j<3 && Pieces[i][j+1] == Blank) {
    			Pieces[i][j+1] = Pieces[i][j];
    			Pieces[i][j] = 0;
    			flag = Check(i, j+1);
    		}
    		break;
    	case North:
    		if (i>0 && Pieces[i-1][j] == Blank) {
    			Pieces[i-1][j] = Pieces[i][j];
    			Pieces[i][j] = 0;
    			flag = Check(i-1, j);
    		}
    		break;
    	case West:
    		if (j>0 && Pieces[i][j-1] == Blank) {
    			Pieces[i][j-1] = Pieces[i][j];
    			Pieces[i][j] = 0;
    			flag = Check(i, j-1);
    		}
    		break;
    	case South:
    		if (i<3 && Pieces[i+1][j] == Blank) {
    			Pieces[i+1][j] = Pieces[i][j];
    			Pieces[i][j] = 0;
    			flag = Check(i+1, j);
    		}
    	}

    	return flag;
    }
   */
    
    
    
    
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	super.onCreateOptionsMenu(menu);
    	menu.add(0,ITEM0,0,"MainMenu");
    	menu.add(0,ITEM1,0,"Exit");
    	menu.findItem(ITEM1);
    	return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch (item.getItemId()) {
		case ITEM0:
			actionClickMenuItem1();
			break;
		case ITEM1:
			actionClickMenuItem2();
			break;

		default:
			break;
		}
    	
    	return super.onOptionsItemSelected(item);
    }
    
    private void actionClickMenuItem1(){
    	setContentView(R.layout.startoptions);
    	AddStartOptions();
    	
    }
    
    private void actionClickMenuItem2(){
    	finish();    	
    }
  
  
}

